﻿// Copyright @ shch8.com All Rights Reserved At 2008-12-16
//开发：商创技术（www.shch8.com）望月狼
/*
·子对象控制

 例：
 import com._public._control;
 ChildCtrl.getInstance().removeAll(editArea);//清空子对象

 */
package com._public._control
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	public class ChildCtrl {
		private static var Instance:ChildCtrl;
		private var layerNum:uint;
		private var getObject:Object;
		private var ctrlClip:Object;
		public static function getInstance():ChildCtrl {
			if (Instance == null) {
				Instance = new ChildCtrl;
				}
			return Instance;
		}
		/*清空子对象*/
		public function removeAll(removeObj:Object):void {
			if(removeObj==null)return
			if(removeObj.numChildren==0)return
			layerNum = 0;
			ctrlClip = removeObj
			removeFun();
		}
		/*移到最顶层*/
		public function layerTop(parentObj:Object,moveObj:Object):void {
			parentObj.setChildIndex(moveObj, parentObj.numChildren - 1);
		}
		/**
		 * 图片大小自适应调整
		 * @param	img	图片
		 * @param	wid 宽
		 * @param	hei 高
		 */
		public function resizeImg(img:Object,wid:Number,hei:Number):void
		{
			var widthProportion:Number = img.height / img.width;
			var heightProportion:Number = img.width / img.height;
			var _stageProportion:Number = hei / wid;
			if (widthProportion > _stageProportion){
			img.width = wid;
			img.height = widthProportion * wid;
			}else{
			img.width = heightProportion * hei;
			img.height = hei;
			}
		}
		private function removeFun():void {
			getObject = ctrlClip.getChildAt(layerNum);
			if (getObject != null) {
				getObject.addEventListener(Event.REMOVED,removeNext)
				ctrlClip.removeChild(getObject);
			}else {
				clearObj()
				}
		}
		private function removeNext(event:Event=null):void {
			layerNum++;
			getObject.removeEventListener(Event.REMOVED, removeNext);
			if (layerNum < ctrlClip.numChildren) {
				removeFun();
			}else {
				clearObj()
				}
		}
		private function clearObj():void {
			Instance =null
			getObject=ctrlClip = null
		}
	}
}